import { buildBatchActions, createPageTemplateConfig, PageTemplate, type BatchActionConfig } from '@/components';
import { CheckOutlined, CloseOutlined, DeleteOutlined } from '@ant-design/icons';
import { message } from 'antd';
import { pageConfig, type AduitRecord } from './pageConfig';

const AduitManagePage = () => {
  // 统一的操作处理函数
  const handleAction = (actionType: string, record: AduitRecord) => {
    console.log('Action triggered:', actionType, record);

    switch (actionType) {
      case 'view':
        message.info(`查看审核记录 ${record.aduitPage} 的详情`);
        break;
      case 'edit':
        message.info(`编辑审核记录 ${record.aduitPage}`);
        break;
      case 'approve':
        message.success(`已通过审核 ${record.aduitPage}`);
        break;
      case 'reject':
        message.warning(`已拒绝审核 ${record.aduitPage}`);
        break;
      case 'delete':
        message.warning(`删除审核记录 ${record.aduitPage}`);
        break;
      default:
        message.error(`未知操作: ${actionType}`);
    }
  };

  // 批量操作配置
  const batchActionConfigs: BatchActionConfig[] = [
    {
      key: 'approve',
      label: '批量通过',
      icon: <CheckOutlined />,
      type: 'primary',
      action: async (selectedRows: AduitRecord[]) => {
        const pendingRows = selectedRows.filter((row) => row.aduitStatus === 'pending');
        if (pendingRows.length !== selectedRows.length) {
          message.warning('只能批量通过待审核的记录');
        }

        if (pendingRows.length > 0) {
          console.log(
            '批量通过审核:',
            pendingRows.map((r) => r.aduitPage),
          );
          message.success(`已通过 ${pendingRows.length} 条审核记录`);
        }
      },
    },
    {
      key: 'reject',
      label: '批量拒绝',
      icon: <CloseOutlined />,
      danger: true,
      action: async (selectedRows: AduitRecord[]) => {
        const pendingRows = selectedRows.filter((row) => row.aduitStatus === 'pending');
        if (pendingRows.length !== selectedRows.length) {
          message.warning('只能批量拒绝待审核的记录');
        }

        if (pendingRows.length > 0) {
          console.log(
            '批量拒绝审核:',
            pendingRows.map((r) => r.aduitPage),
          );
          message.success(`已拒绝 ${pendingRows.length} 条审核记录`);
        }
      },
    },
    {
      key: 'delete',
      label: '批量删除',
      icon: <DeleteOutlined />,
      danger: true,
      action: async (selectedRows: AduitRecord[]) => {
        console.log(
          '批量删除审核记录:',
          selectedRows.map((r) => r.aduitPage),
        );
        message.success(`已删除 ${selectedRows.length} 条审核记录`);
      },
    },
  ];

  // 使用配置构建器创建页面配置
  const templateConfig = createPageTemplateConfig<AduitRecord>()
    .setSearchConfig({
      fields: pageConfig.searchFields,
    })
    .setTableConfig({
      columns: pageConfig.columns,
      rowKey: 'paramIndex',
      props: {
        scroll: { x: 1200 },
        size: 'middle',
      },
    })
    .setFormConfig({
      fields: pageConfig.formFields,
    })
    .setActionConfig({
      permissions: {
        create: true,
        update: true,
        delete: true,
        view: true,
      },
      onAction: handleAction,
    })
    .setBatchActionConfig({
      enabled: true,
      renderCustom: (selectedRowKeys, selectedRows) =>
        buildBatchActions(selectedRowKeys, selectedRows, batchActionConfigs),
    })
    .setCrudConfig(pageConfig.crudConfig)
    .build();

  return <PageTemplate<AduitRecord> {...templateConfig} />;
};

export default AduitManagePage;
